package org.zyl.util.file;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 文件工具类
 * @author yzzhouyalei@foxmail.com
 * @time 2019-01-11 11:25:11
 */
public class FileUtil {

	/**
	 * 写入文件
	 * @author yzzhouyalei@foxmail.com
	 * @time 2019-01-11 11:25:40
	 */
	public static void outFile(String path, String filename, String content) {
		FileOutputStream fop = null;
		File file;
		try {
			dirExists(new File(path));

			file = new File(path + filename);
			fop = new FileOutputStream(file);

			if (!file.exists()) {
				file.createNewFile();
			}
			byte[] contentInBytes = content.getBytes();
			fop.write(contentInBytes);
			fop.flush();
			fop.close();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (fop != null) {
					fop.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 创建多级目录
	 * @author yzzhouyalei@foxmail.com
	 * @time 2019-01-11 11:25:22
	 */
	public static void dirExists(File file) {
		if (file.exists()) {
			if (file.isDirectory()) {
				System.out.println("dir exists");
			} else {
				System.out.println("the same name file exists, can not create dir");
			}
		} else {
			System.out.println("dir not exists, create it ...");
			file.mkdirs();
		}
	}
}
